<?php
/**
 * View_Special_Building
 *
 * Класс View_Special_Building отвечает за вывод информации, связанной со зданиями
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class View_Special_Building extends View_Base {
	
	/**
	 * Выводит ресурсы места
	 * 
	 */
	public static function showPlaceBuildings() {
		$objGeneral = Game_General::instance();
		$arrPositionsWithBuildings = array();
		foreach ($objGeneral->objPlace->getBuildingsData() as $buildingNum => $arrBuilding) {
			//echo Game_Resource::getResourceName($resourceId) . ': ' . $resourceValue . '<br/>';
			echo '<p><a href="' . Game_PlaceBuilding::getPlaceBuildingUrlByPositionId($arrBuilding['ID_position']) . '">' . Game_Building::getBuildingName($arrBuilding['ID_building']) . '</a>: ' . $arrBuilding['level'] . '</p>';
			$arrPositionsWithBuildings[$arrBuilding['ID_position']] = 1;
		}
		echo '<br />';
		for ($i = 1; $i <= 40; $i++) {
			if (empty($arrPositionsWithBuildings[$i]))	echo '<p><a href="' . W_ABSPATH . 'building/create/' . $i . '/">' . Service_Translate::getText('Построить новое здание в позиции ') . $i . '</a></p>';
		}
	}
	
	/**
	 * Выводит информацию о строении
	 * 
	 * @param Game_PlaceBuilding $objPlaceBuiding - информация о текущем состоянии здания в месте
	 */
	public static function showBuildingPage(Game_PlaceBuilding $objPlaceBuiding) {
		//$objResource = new Game_Resource($objBuilding);
		$buildingName = $objPlaceBuiding->objBuilding->getBuildingName($objPlaceBuiding->objBuilding->id);
		echo '<div class="panel panel-default">';
			echo '<div class="panel-heading lead">';
				//echo '<h2>';
					echo $buildingName;
					echo ' ' . Service_Translate::getText('уровень') . ' ' . $objPlaceBuiding->getField('level');
					//echo '</h2>';
			echo '</div>';
			echo '<div class="panel-body">';
		
		// Вывод описания
		echo '<img src="' . W_IMAGES . 'dot.gif" alt="' . $buildingName . '" class="building ' . $objPlaceBuiding->objBuilding->getField('building_name') . '" />';
		echo '<div class="bottom-separator">';
			echo $objPlaceBuiding->objBuilding->getDescription();
		echo'</div>';
			
		// Вывод информации о свойствах здания
		$objConcreteBuilding = Game_Building_ItemCreator::initBuildingItem($objPlaceBuiding->objBuilding);
		$arrPropertiesInfo = $objConcreteBuilding->getPropertiesInfo($objPlaceBuiding->getField('level'));
		self::showPropertiesInfo($arrPropertiesInfo);
		
		$nextLevel = $objPlaceBuiding->getLevelToBuild();
		// Если можно построить следующий уровень
		if ($nextLevel) { 
				// Вывод цены строительства
				echo '<div class="bottom-separator">';
					echo '<span>' . Service_Translate::getText('Улучшить до уровня') . ' ' . $nextLevel . '</span>';
					$objResourcesCost = $objPlaceBuiding->objBuilding->getBuildingLevelUpgradeCost($nextLevel);
					$buildingTime = $objPlaceBuiding->getBuildingTime();
					$arrConsumption = $objPlaceBuiding->getConsumptionCost();
					View_Special_Resource::showCost($objResourcesCost, $buildingTime, $arrConsumption);
				echo '</div>';
				
				// Вывод блока "Начала строительства"
				echo '<div>';
					$isAvailableToUpgrade = $objPlaceBuiding->isAvailableToUpgrade();
					if (!$isAvailableToUpgrade) {
						$secondToEnoughResources = $objPlaceBuiding->calculateSecondToEnoughResources($nextLevel);
						if ($secondToEnoughResources > 0) { // Должно стоять именно > 0. -1 Означает, что складов не хватает
							echo Service_Translate::getText('Ресурсов будет достаточно через ') . Service_date::secondsToHumanFormat($secondToEnoughResources);
						}
					}
	
					$arrBuildingEvents =  $objPlaceBuiding->getBuildingEvents();
					foreach ($arrBuildingEvents as $arrBuildingEvent) View_Special_Building::showBuildingEvent($arrBuildingEvent);
					
					// Кнопка строительства следующего уровня
					echo '<p>';
						echo '<a class="btn btn-primary' . (!$isAvailableToUpgrade ? ' disabled' : null) . '" href="' . Game_PlaceBuilding::getPlaceBuildingUrlByPositionId($objPlaceBuiding->getField('ID_position')) . '?upgrade=' . $nextLevel . '">';
							echo Service_Translate::getText('Улучшить до уровня') . ' ' . $nextLevel;
						echo '</a>';
					echo '</p>';
				echo '</div>';
		} 
			echo '</div>';
		echo '</div>';
	}
	
	/**
	 * Выводит список зданий, которые могут быть построены
	 * 
	 * @param array $arrBuildings - список зданий
	 * @param int $positionId - позиция для строительства
	 */
	public static function showListCouldBeCreate(array $arrBuildings, $positionId) {
		if (!$arrBuildings) {
			echo '<p>' . Service_Translate::getText('В данный момент нельзя построить новое здание, возможно, вы можете улучшить старое') . '</p>';
			return false;
		}
		$objGeneral = Game_General::instance();
		$objPlaceBuilding = new Game_PlaceBuilding($objGeneral->objPlace->id, $positionId);
		$objPlaceBuilding->setField('level', 0);
		
		
		$i = 0;
		foreach ($arrBuildings as $objBuildingStatus) {
			if ($objBuildingStatus->isBuilt()) continue;
			$arrBuildingsUnperformedDependences = $objBuildingStatus->getBuildingsUnperformedDependences();
			$isNeedMoreBuildings = !empty($arrBuildingsUnperformedDependences);
			$objBuildingCreateInfo = $objBuildingStatus->getBuildingCreateInfo();
			$buildingId = $objBuildingCreateInfo->id;
			
			
			// Получаем инфу, можно ли построить здание
			$objPlaceBuilding->objBuilding = $objBuildingCreateInfo;
			$objPlaceBuilding->setField('ID_building', $buildingId);
			$isAvailableToCreate = $objPlaceBuilding->isAvailableToCreate();
			
			if ($i++) echo '<p>============================</p>';
			echo '<p>';
				if ($isAvailableToCreate) {
					echo '<a href="' . Game_PlaceBuilding::getPlaceBuildingUrlToBeCreate($buildingId, $positionId) . '">';
						echo Game_Building::getBuildingName($buildingId);
					echo '</a>';
				}
				else echo '<span>' . Game_Building::getBuildingName($buildingId) . '</span>';
				$objResourcesCost = $objBuildingCreateInfo->objResources;
				$buildingTime = Game_Building::getBuildingTime($objBuildingCreateInfo->getField('time_base'), $objBuildingCreateInfo->getField('time_additional'), $objBuildingCreateInfo->getField('time_coeff'));;
				$arrConsumption = Game_ResourceWithProduction::getBuildingConsumption($objBuildingCreateInfo->tableFields, 1);
				View_Special_Resource::showCost($objResourcesCost, $buildingTime, $arrConsumption);
				
				if ($isNeedMoreBuildings) {
					echo '<p>';
						echo '<b>' . Service_Translate::getText('Требуется построить') . '</b>: ';
						$k = 0;
						foreach ($arrBuildingsUnperformedDependences as $arrNeedToBuild) {
							if ($k++) echo ', ';
							echo Game_Building::getBuildingName($arrNeedToBuild['ID_building_need']) . ' ' . $arrNeedToBuild['level'];
						}
					echo '</p>';
				}
				
			echo '</p>';
		}
	}


	

	/**
	 * Выводит свойства здания
	 * 
	 * @param array $arrPropertiesInfo
	 */
	public static function showPropertiesInfo(array $arrPropertiesInfo) {
		if (!reset($arrPropertiesInfo)) return false;
		
		echo '<div class="well" style="width:50%;">';
			foreach ($arrPropertiesInfo as $arrLevelProperties) {
				echo '<p><b>' . $arrLevelProperties['header'] . ':</b> ' . $arrLevelProperties['info'] . '</p>';
			}
		echo '</div>';
	}
	
	/**
	 * Выводит событие строительства здания
	 * 
	 * @param array $arrBuildingEvent - массив с информацией о событии
	 */
	public static function showBuildingEvent(array $arrBuildingEvent) {
		$objGeneral = Game_General::instance();
		$buildingId = $objGeneral->objPlace->getBuildingIdByPosition($arrBuildingEvent['ID_essence']);
		echo '<p>';
			echo Service_Translate::getText('Постройка здания') . ' ';
			echo Game_Building::getBuildingName($buildingId);
			echo ' ' . Service_Translate::getText('до уровня') . ' ';
			echo $arrBuildingEvent['amount'];
			echo ' ' . Service_Translate::getText('осталось времени') . ' ';
			echo Service_date::secondsToHumanFormat(Service_Date::timeLeft($arrBuildingEvent['event_time']));
			
		'</p>';
	} 
}
?>